/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiu.accesslog;

import edu.fiu.accesslog.filter.AccessLogFilter;
import edu.fiu.accesslog.parser.ApacheParserImpl;
import edu.fiu.accesslog.parser.IParser;
import edu.fiu.accesslog.statistics.LogStatistics;
import java.io.*;
import java.text.NumberFormat;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Hasan Mahmud @date Jun 18, 2012
 */
public class LogInputReader extends TimerTask
{

        String format;
        String[] logFileNames;
        private Timer timer;
        private double totalFileSize = 0;
        private double processedFileSize = 0;
        private final Object progressReportLock = new Object();
        private static final int PROGRESS_REPORT_DELAY = 10 * 1000; //second
        private static final int PROGRESS_REPORT_COUNTER = 10000; //second
        String line1 = "178.33.167.6 - - [14/May/2012:05:57:52 -0400] \"GET  HTTP/1.0\" 400 306 \"-\" \"-\"";
        String singleLine;
        private String preProcessedFileName = "preProcessedLog";
        private long startTime;

        PrintWriter outputStream = null;
        
        public LogInputReader(String[] logFileNames, String format , String preProcessedFileName)
        {
                this.logFileNames = logFileNames;
                this.format = format;
                timer = new Timer("Showin g Percentage");
                this.preProcessedFileName = preProcessedFileName;
        }

        public void startReading()
        {
                LogStatistics stat = LogStatistics.getInstance();
                timer.schedule(this, 2000, PROGRESS_REPORT_DELAY);
                AccessLogFilter alFilter = AccessLogFilter.getInstance();

                startTime = System.currentTimeMillis();
                IParser formatter = null;
                try
                {

                        formatter = (IParser) new ApacheParserImpl();
                        formatter.setFormat(format);
                        for (String logFileName : logFileNames)
                        {
                                File file = new File(logFileName);
                                totalFileSize += file.length();
                        }
                        //System.out.println();

                        long progressedSize = 0;
                        SingleAccessLog singleAccess;

                        //singleAccess = formatter.parse(line1, format);
                        //stat.addAccessLogEntry(singleAccess);
                        outputStream =    new PrintWriter(new FileWriter(preProcessedFileName));
                        for (String logFileName : logFileNames)
                        {
                                try
                                {
                                        FileInputStream fstream = new FileInputStream(logFileName);
                                        // Get the object of DataInputStream
                                        DataInputStream in = new DataInputStream(fstream);
                                        BufferedReader br = new BufferedReader(new InputStreamReader(in));

                                        //Read File Line By Line
                                        long i = 0;
                                        while ((singleLine = br.readLine()) != null)
                                        {

                                                i++;

                                                singleAccess = formatter.parse(singleLine, format);
                                                progressedSize += singleLine.length();

                                                if(! alFilter.isFiltered(singleAccess))
                                                {
                                                        stat.addAccessLogEntry(singleAccess);
                                                        outputStream.println(singleLine);
                                                }

                                                if (i > PROGRESS_REPORT_COUNTER)
                                                {
                                                        i = i % PROGRESS_REPORT_COUNTER;
                                                        synchronized (progressReportLock)
                                                        {
                                                                processedFileSize += progressedSize;
                                                                progressedSize = 0;
                                                        }

                                                }



                                        }

                                        //Close the input stream
                                        br.close();
                                        in.close();
                                        fstream.close();
                                }
                                catch (Exception ex)
                                {
                                        ex.printStackTrace();
                                }
                                finally
                                {
                                        timer.cancel();
                                }
                        }

                        outputStream.close();
                }
                catch (Exception e)
                {
                        e.printStackTrace();

                }
                long finishTime = (System.currentTimeMillis() - startTime) / 1000;
                System.out.println("Finished parsing in: " + finishTime + "  seconds");

        }
        //private double prevProcessedFileSize = 0;

        @Override
        public void run()
        {

                try
                {
                        synchronized (progressReportLock)
                        {

                                NumberFormat fmt = NumberFormat.getPercentInstance();
                                System.out.println("Progress:  " + fmt.format(processedFileSize / totalFileSize));
                                //prevProcessedFileSize = processedFileSize;
                        }


                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }

        }
}
